<template>
  <el-container class="layout-container">
    <el-aside :width="isCollapse ? '64px' : '200px'" class="aside">
      <div class="logo" @click="toggleCollapse">
        <img src="@/assets/images/logo-3.jpg" alt="Logo" />
        <span class="platform-name">物联网数据平台</span>
      </div>
      <el-menu
        :default-active="activeMenu"
        class="el-menu-vertical"
        :router="true"
        :collapse="isCollapse"
      >
        <el-menu-item index="/user/dashboard">
          <el-icon><Monitor /></el-icon>
          <template #title>大屏展示</template>
        </el-menu-item>
        
        <el-menu-item index="/user/devices">
          <el-icon><Connection /></el-icon>
          <template #title>设备管理</template>
        </el-menu-item>
        
        <el-menu-item index="/user/mqtt" v-if="isHighLevelUser">
          <el-icon><Message /></el-icon>
          <template #title>MQTT管理</template>
        </el-menu-item>
        
        <el-menu-item index="/user/data-center">
          <el-icon><DataLine /></el-icon>
          <template #title>数据中心</template>
        </el-menu-item>
        
        <el-menu-item index="/user/data-items" v-if="isHighLevelUser">
          <el-icon><DataBoard /></el-icon>
          <template #title>数据项管理</template>
        </el-menu-item>
        
        <el-menu-item index="/user/system" v-if="isHighLevelUser">
          <el-icon><Setting /></el-icon>
          <template #title>系统设置</template>
        </el-menu-item>
      </el-menu>
    </el-aside>
    
    <el-container class="main-container">
      <el-header class="header">
        <div class="header-left">
          <el-icon 
            class="collapse-btn"
            @click="toggleCollapse"
          >
            <Fold v-if="!isCollapse" />
            <Expand v-else />
          </el-icon>
          <div v-if="deviceStore.currentDevice" class="current-device">
            <el-icon><Monitor /></el-icon>
            <span>{{ deviceStore.currentDevice.equipment_name }}</span>
          </div>
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{ currentRoute }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        
        <div class="header-right">
          <el-dropdown trigger="click" @command="handleCommand">
            <span class="user-dropdown">
              <div v-if="deviceStore.currentDevice" class="current-device">
             <!--     <<el-icon><Monitor /></el-icon>>-->
              <!-- <span>{{ deviceStore.currentDevice.equipment_name }}</span> -->
              </div>
              <el-avatar :size="32" :src="userAvatar">
                {{ userInitials }}
              </el-avatar>
              <span class="username">{{ username }}</span>
              <el-icon><CaretBottom /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">
                  <el-icon><User /></el-icon>个人信息
                </el-dropdown-item>
                <el-dropdown-item command="settings">
                  <el-icon><Setting /></el-icon>设置
                </el-dropdown-item>
                <el-dropdown-item divided command="logout">
                  <el-icon><SwitchButton /></el-icon>退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore } from '../store/authStore'
import { useDeviceStore } from '../stores/deviceStore'

import {
  Monitor,
  Connection,
  Message,
  DataLine,
  Setting,
  Fold,
  Expand,
  CaretBottom,
  User,
  SwitchButton,
  TrendCharts
} from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()
const deviceStore = useDeviceStore()

const isCollapse = ref(false)
const activeMenu = computed(() => route.path)

// 获取用户信息
const username = computed(() => {
  const userInfo = authStore.getUserInfo
  return userInfo?.nickname || userInfo?.name || '用户'
})

const userInitials = computed(() => {
  const userInfo = authStore.getUserInfo
  return (userInfo?.nickname || userInfo?.name || '用户').charAt(0).toUpperCase()
})

const userAvatar = computed(() => {
  const userInfo = authStore.getUserInfo
  return userInfo?.avatar || ''
})

// 判断是否为高级用户（LV1或LV2）
const isHighLevelUser = computed(() => {
  const userRole = authStore.getUserRole
  return userRole === 'LV1' || userRole === 'LV2'
})

// 当前路由名称
const currentRoute = computed(() => {
  const routeMap = {
    '/user/dashboard': '大屏展示',
    '/user/devices': '设备管理',
    '/user/mqtt': 'MQTT管理',
    '/user/data-center': '数据中心',
    '/user/system': '系统设置',
    '/user/data-items': '数据项管理',
    '/user/profile': '个人信息'
  }
  return routeMap[route.path] || route.name || '首页'
})

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const handleCommand = async (command) => {
  switch (command) {
    case 'profile':
      router.push('/user/profile')
      break
    case 'settings':
      router.push('/user/system')
      break
    case 'logout':
      try {
        authStore.logout()
        router.push('/user/login')
      } catch (error) {
        console.error('Logout failed:', error)
      }
      break
  }
}

onMounted(() => {
  deviceStore.initialize()
})
</script>

<style scoped>
.layout-container {
  height: 100vh;
  background-color: #f5f7fa;
}

.aside {
  background-color: #1a1a1a; /* 黑色背景 */
  color: #ffd700; /* 金色文字 */
  overflow: hidden;
  transition: width 0.3s ease;
  box-shadow: 4px 0 10px rgba(0, 0, 0, 0.5); /* 3D 阴影效果 */
  border-right: 2px solid #ffd700; /* 金色边框 */
}

.logo {
  display: flex;
  align-items: center;
  padding: 16px;
  color: #ffd700; /* 金色文字 */
  transition: all 0.3s ease;
  background: #1a1a1a; /* 黑色背景 */
  overflow: hidden;
  cursor: pointer;
}

.logo img {
  width: 32px;
  height: 32px;
  margin-right: 12px;
  transition: all 0.3s ease;
}

.platform-name {
  font-size: 18px;
  font-weight: 600;
  white-space: nowrap;
  transition: all 0.3s ease;
  color: #ffd700; /* 金色文字 */
}

.el-menu-vertical {
  border-right: none;
  background-color: #1a1a1a; /* 黑色背景 */
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 200px;
}

.header {
  background-color: #fff;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 64px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.header-logo {
  display: flex;
  align-items: center;
  margin-right: 16px;
}

.header-logo img {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 8px;
}

.header-logo span {
  font-size: 16px;
  font-weight: 600;
  color: #1a1a1a;
  white-space: nowrap;
}

.collapse-btn {
  font-size: 20px;
  cursor: pointer;
  margin-right: 24px;
  color: #1e1e2d;
  transition: color 0.3s ease;
}

.collapse-btn:hover {
  color: #409eff;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-dropdown {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 0 8px;
  height: 48px;
  transition: all 0.3s ease;
  border-radius: 4px;
}

.user-dropdown:hover {
  background: rgba(0, 0, 0, 0.025);
}

.username {
  margin: 0 8px;
  color: #1e1e2d;
  font-weight: 500;
}

.el-main {
  background-color: #f6f6f7;
  padding: 24px;
}

:deep(.el-menu-item) {
  height: 50px;
  line-height: 50px;
  color: #a3a3b5;
  margin: 4px 0;
  padding: 0 20px !important;
  transition: all 0.3s ease;
}

:deep(.el-menu-item.is-active) {
  background-color: #333333 !important; /* 深灰色背景 */
  color: #ffd700 !important; /* 金色文字 */
  border-right: 3px solid #ffd700; /* 金色边框 */
}

:deep(.el-menu-item:hover) {
  background-color: #333333 !important; /* 深灰色背景 */
  color: #ffd700 !important; /* 金色文字 */
}

:deep(.el-menu) {
  background-color: #1a1a1a; /* 黑色背景 */
  padding: 8px 0;
}

:deep(.el-menu-item .el-icon) {
  font-size: 18px;
  margin-right: 10px;
  width: 24px;
  height: 24px;
  color: #ffd700; /* 金色图标 */
}

:deep(.el-breadcrumb__inner) {
  color: #1e1e2d !important;
  font-weight: 500;
}

:deep(.el-breadcrumb__item:last-child .el-breadcrumb__inner) {
  color: #909399 !important;
  font-weight: 400;
}

:deep(.el-avatar) {
  background-color: #e6f7ff;
  color: #409eff;
  font-weight: 600;
}

.current-device {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: var(--el-color-primary-light-9);
  border-radius: 4px;
  color: var(--el-color-primary);
  font-size: 14px;
}

.current-device .el-icon {
  font-size: 16px;
}

/* 折叠时的样式 */
.aside:has(.el-menu--collapse) {
  width: 64px !important;
}

.aside:has(.el-menu--collapse) .logo {
  padding: 0 14px;
}

.aside:has(.el-menu--collapse) .logo span {
  opacity: 0;
  width: 0;
  margin: 0;
}

.aside:has(.el-menu--collapse) .logo img {
  margin-right: 0;
}

:deep(.el-menu--collapse) .el-menu-item {
  padding: 0 14px !important;
}
</style>